package com.company.consumer.controller;

import com.company.api.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class GoodsConsumerContrller {

    /**
     * http URL
     */
    private static final String REST_URL_PREFIX_HTTP = "http://localhost:8001";

    /**
     * 通过应用名称的访问地址（应用名称等同于IP + 端口）
     */
    private static final String REST_URL_PREFIX_APPLICATION_SERVICE_NAME = "http://MICROSERVICECLOUD-GOODS";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/goods/list/http")
    public List<Goods> getGoodsHttp() {
        return restTemplate.exchange(REST_URL_PREFIX_HTTP + "/goods/list", HttpMethod.GET,
                null,   new ParameterizedTypeReference<List<Goods>>(){}).getBody();
    }

    @GetMapping("/goods/list/applicationName")
    public List<Goods> getGoodsServiceName() {
        return restTemplate.exchange(REST_URL_PREFIX_APPLICATION_SERVICE_NAME + "/goods/list", HttpMethod.GET,
             null,   new ParameterizedTypeReference<List<Goods>>(){}).getBody();
    }

    @GetMapping("/goods/list/hystrix")
    public List<Goods> getGoodsHystrix() {
        return restTemplate.exchange(REST_URL_PREFIX_APPLICATION_SERVICE_NAME + "/goods/list/hystrix", HttpMethod.GET,
                null,   new ParameterizedTypeReference<List<Goods>>(){}).getBody();
    }
}
